package com.xmhmyh.generalframework.project.sys.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xmhmyh.generalframework.common.BaseController;
import com.xmhmyh.generalframework.common.entity.Response;
import com.xmhmyh.generalframework.project.sys.dto.CascaderDTO;
import com.xmhmyh.generalframework.project.sys.entity.Location;
import com.xmhmyh.generalframework.project.sys.enums.LocationLvl;
import com.xmhmyh.generalframework.project.sys.service.ILocationService;
import org.apache.commons.collections.CollectionUtils;
import org.nustaq.kson.Kson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Collection;
import java.util.List;

/**
 * @Author: 于丁
 * @Description:
 * @DateTime: 2020/8/14 10:43 上午
 * @Params: * @param: null
 * @Return {@link null}
 */
@RestController
@RequestMapping("/sys/location")
public class LocationController extends BaseController {

    @Autowired
    private ILocationService locationService;


    @GetMapping("setLocation")
    public Response setLocationHandler(@PathParam("code") String code, @PathParam("name") String name) {
        Location location = locationService.getById(code);
        location.setName(name);
        locationService.updateById(location);
        return Response.success(location);
    }

    @PostMapping("query")
    public Response queryHandler(@RequestParam String selectedCode) {
        Collection<Location> locationList = locationService.queryByParentCode(selectedCode);
        JSONArray jsonArray = new JSONArray();
        if (CollectionUtils.isNotEmpty(locationList)) {
            for (Location location : locationList) {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("name", location.getName());
                jsonObj.put("code", location.getCode());
                jsonObj.put("lvl", location.getLvl());
                jsonArray.add(jsonObj);
            }
        }
        return Response.success(jsonArray);
    }

    @PostMapping("querySelect")
    public Response querySelectHandler(@RequestParam List<String> selectId) {
        List<CascaderDTO> cascaderDTOList = locationService.querySelectHandler(selectId);
        return Response.success(cascaderDTOList);
    }


    @GetMapping("getLocationData")
    public Response getLocationData() {
        return Response.success(dg("0"));
    }

    public JSONArray dg(String par_id) {
        JSONArray jsonArray = new JSONArray();
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Location::getParentCode, par_id);
        List<Location> cou_location = locationService.list(queryWrapper);
        for (Location location : cou_location) {
            JSONObject object = new JSONObject();
            object.put("value", location.getCode());
            object.put("label", location.getName());
            if (location.getLvl() <= LocationLvl.DISTRICT.getValue()) {
                JSONArray jsonArray2 = dg(location.getCode());
                if (jsonArray2.size() > 0) {
                    object.put("children", jsonArray2);
                }
                System.out.println(location.getName() + "==" + location.getCode() + "执行完毕===============");
            }
            jsonArray.add(object);
        }
        return jsonArray;
    }
}
